import config from "/config.js";
let total = function(arr) {
    var s = 0;
    arr.forEach(function(val, idx, arr) {
        s += val;
    }, 0);
    return s;
};
/**
 * 时间格式化
 * @param {object,string} date = 时间
 * @param {string} format = 'yyyy-MM-dd'
 */
export const dateFilter = (date, format = "yyyy-MM-dd") => {
    // format: string -> yyyy-MM-dd hh:mm:ss
    if (!date) return "暂无时间";
    if (date.length < 20) {
        date = date.replace(/-/g, "/").replace(/\./g, "/");
    }
    let d = new Date(date);
    let year = d.getFullYear();
    let month = (d.getMonth() + 1).toString().padStart(2, 0);
    let day = d
        .getDate()
        .toString()
        .padStart(2, 0);
    let hour = d
        .getHours()
        .toString()
        .padStart(2, 0);
    let minute = d
        .getMinutes()
        .toString()
        .padStart(2, 0);
    let second = d
        .getSeconds()
        .toString()
        .padStart(2, 0);

    let weekend = ["天", "一", "二", "三", "四", "五", "六"];
    let week = d.getDay();
    return format
        .replace("yyyy", year)
        .replace("MM", month)
        .replace("dd", day)
        .replace("hh", hour)
        .replace("mm", minute)
        .replace("ss", second)
        .replace("week", "星期" + weekend[week]);
};
/**
 * 路径过滤补全
 * @param {string} url = '字符串'
 */
export const imgFilter = (url) => {
    if (!url) return "";
    if (url.includes("http") || url.includes("static")) return url;
    let baseUrl = config.baseUrl;
    return baseUrl + url;
};
/**
 * 手机号脱敏处理，保留前三后四
 * @param {string} value = 传入手机号
 */
export const phoneHide = (value) => {
    if (!value) return "";
    let str = value;
    str = str.replace(new RegExp("[^0-9]+", "g"), "");
    if (str.length == 11) {
        str = str.toString().replace(/(\d{3})\d*(\d{4})/, "$1****$2");
    } else {
        str = str.toString().replace(/(\d{3})\d*(\d{4})/, "$1******$2");
    }
    return str;
};
/**
 * 姓名截取
 * @param {string} name 姓名
 */
export function compellationCut(name) {
    // var reg = /(?<=.)./g;
    // return name.replace(reg, '*');
    return "*" + name.substring(1, name.length);
}
/**
 * 身份证号截取
 * @param {string} card 身份证号
 */
export function idCardCut(card) {
    return card.replace(/^(.{4})(?:\d+)(.{2})$/, "$1****$2");
}
/**
 * 数字转大写
 * @param {string,number} temp = '字符串'
 */
export const numFormatter = (temp) => {
    if (temp === 0) return "零";
    let changeNum = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
    let unit = ["", "十", "百", "千", "万"];
    let strArr = temp
        .toString()
        .split("")
        .reverse();
    let newNum = "";
    if (temp < 100) {
        for (var i = 0; i < strArr.length; i++) {
            newNum =
                // temp为0时,newNum为空
                (i == 0 && strArr[i] == 0 && strArr.length < 3
                    ? ""
                    : // temp为0时,newNum为空
                    i > 0 && strArr[i] == 0 && strArr[i - 1] == 0
                    ? ""
                    : // 10~19以内 不是10
                    strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] != 0
                    ? unit[i] + changeNum[strArr[i - 1]]
                    : // 10~19以内 是10
                    strArr[i] == 1 && strArr[i - 1] <= 9 && strArr[i - 1] == 0 && strArr.length < 3
                    ? unit[i]
                    : // 10~19以外的数字
                      changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + (strArr[i] == 1 && strArr[i - 1] <= 9 ? "" : newNum);
        }
        return newNum;
    } else {
        for (var i = 0; i < strArr.length; i++) {
            newNum = (i == 0 && strArr[i] == 0 ? "" : i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i])) + newNum;
        }
        return newNum;
    }
};
/**
 * 过滤出剩余的秒数 当前时间加上要往后延长的小时数减去记录的时间
 * @param {string,Date} time = 时间
 * @param {number} hour = 小时数 默认0
 * @param {string,Date} currDate = 当前时间最好使用后端服务器的时间 默认js的 new Date()
 */
export const timestamp = (time, hour = 0, currDate= new Date()) => {
    let timestamp = new Date(dateFilter(time, "yyyy/MM/dd hh:mm")).getTime();
    let curr = new Date(dateFilter(currDate, "yyyy/MM/dd hh:mm")).getTime()
    let aDay = 3600 * 1000 * hour;
    return (timestamp + aDay - curr) / 1000;
};
/**
 * 比上期
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 * @param {string} str = '符号'
 */
export const coeff = (newNum, oldNum, str = "") => {
    let a = Number(Math.abs(newNum - oldNum));
    if (isNaN(a)) return "";
    let b = Boolean(a > 10000) ? Number(a / 10000).toFixed(2) + "万" : a.toFixed(2);
    return b + str;
};
/**
 * 正负数
 * @param {number} newNum = '本期数'
 * @param {number} oldNum = '上期数'
 */
export const sign = (newNum, oldNum) => {
    // if(newNum-oldNum===0) return true
    return newNum - oldNum > 0;
};
/**
 * 匹配超额省略加上单位
 * @param {number,string} Num = '源数值'
 */
export const matchingNnit = (Num) => {
    let a = Number(Num);
    let b = a > 10000 ? Number(a / 10000).toFixed(2) + "万" : a.toFixed(2);
    return b;
};

